﻿using AutoMapper;
using DFGK_WMS.Context;
using DFGK_WMS.IRepository;
using DFGK_WMS.Model.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace DFGK_WMS.Repository
{
    public class TCustomerRepository : BaseRepository<TCustomer>, ITCustomerRepository
    {
        public TCustomerRepository(DFGK_DB context, IMapper mapper) : base(context, mapper)
        {
        }

        /// <summary>
        /// 批量添加客户
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public bool AddCustomerList(List<TCustomer> list)
        {
            Context.TCustomers.AddRange(list);
            return Context.SaveChanges()>0;
        }

        public bool AddEXcelshuju(List<TCustomer> customers,out string message)
        {
            using (var shiwu=Context.Database.BeginTransaction())
            {
                try
                {
                    if (customers != null)
                    {
                        message = "批量添加成功";
                        Context.TCustomers.AddRange(customers);
                        Context.SaveChanges();
                       shiwu.Commit();
                       return true;
                    }
                    else
                    {
                        message = "没有数据返回添加";
                        shiwu.Rollback();
                        return false;
                    }
                }
                catch (Exception)
                {

                    throw new Exception("数据错误");
                }
          
            }
           
        }

        public bool AddTCustomer(TCustomer customer)
        {
            try
            {
                //string code = "Customer";
                //var aobj = Context.TCustomers.AsNoTracking().Where(x => x.Code.Contains(code)).OrderByDescending(x => x.CreateDate).FirstOrDefault();
                //if (aobj != null)
                //{
                //    string pattern = @"\d+";
                //    MatchCollection matches = Regex.Matches(aobj.Code, pattern);
                //    string value = matches[matches.Count - 1].Value;
                //    int values = Convert.ToInt32(value);
                //    values++;
                //    customer.Code = code + values.ToString("D4");
                //}
                //else
                //{
                //    int i = 1;
                //    customer.Code = code + i.ToString("D4");
                //}

                customer.CreateDate = DateTime.Now;
                customer.CreateUserId = customer.NowUserId;
                customer.CreateUserName= customer.NowUserRealName;
                var res = Context.TCustomers.Add(customer);
                return Context.SaveChanges() >0;
            }
            catch (Exception ex)
            {

                return false;
            }
        }

        public bool DelTCustomer(int id)
        {
            try
            {
                var cust = Context.TCustomers.Find(id);
                if (cust is not null)
                {
                    Context.TCustomers.Remove(cust);
                    return Context.SaveChanges() > 0;
                }
                return false;
            }
            catch (Exception)
            {

                throw;
            }
        }

        public List<TCustomer> GetAllTCustomer(string? name, int pageIndex, int pageSize, out int totalCount)
        {
            try
            {
                var custList = Context.TCustomers.AsNoTracking().AsQueryable();
                if (!string.IsNullOrEmpty(name))
                {
                    custList = custList.Where(c => c.Name.Contains(name));
                }
                totalCount = custList.Count();
                custList = custList.OrderBy(x => x.DisplayOrder).Skip((pageIndex - 1) * pageSize).Take(pageSize);
                return custList.ToList();
            }
            catch (Exception)
            {

                throw;
            }
        }

        public TCustomer GetCustomerById(int id)
        {
            try
            {
                return Context.TCustomers.Find(id);
            }
            catch (Exception)
            {

                throw;
            }
        }

        public List<TCustomer> GetCustomerSelect(string? name)
        {
            try
            {
                var custList = Context.TCustomers.AsQueryable();
                if (!string.IsNullOrEmpty(name))
                {
                    custList = custList.Where(c => c.Name.Contains(name));
                }
                return custList.ToList();
            }
            catch (Exception)
            {
                throw;
            }
        }

        public List<TDictItem> GetTCustomerDic()
        {
            try
            {
                int dicTypeId = 0;
                var dicType = Context.TDictTypes.Where(c => c.TypeName == "客户类型");
                var resList = new List<TDictItem>();
                if (dicType.Count() != 0)
                {
                    dicTypeId = dicType.First().Id;
                    foreach (var item in Context.TDictItems)
                    {
                        if (item.TypeId == dicTypeId)
                            resList.Add(item);
                    }
                }
                return resList;
            }
            catch (Exception)
            {

                throw;
            }
        }

        public bool UpdTCustomer(TCustomer Customer)
        {
            try
            {
                Customer.ModifyDate = DateTime.Now;
                Customer.ModifyUserId = Customer.NowUserId;
                Customer.ModifyUserName = Customer.NowUserRealName;
                Context.TCustomers.Update(Customer);
                return Context.SaveChanges() > 0;
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
